197. Rising Temperature


Posted by ikl258794613 on 2024-02-22

Column Name Type
id int
recordDate date
temperature int

id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

Input:
Weather table:

id recordDate temperature
1 2015-01-01 10
2 2015-01-02 25
3 2015-01-03 20
4 2015-01-04 30

Output:

id
2
4

Explanation:
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).

解答:

SELECT w1.id FROM Weather w1, Weather w2 
WHERE DATEDIFF(w1.recordDate,w2.recordDate) = 1 
AND w1.temperature > w2.temperature

學習點:
1.自己跟自己比較時SELECT自己兩次。
2.DATEDIFF ( datepart , startdate , enddate ) datepart不傳時返回兩個日期之間的差異。


#SQL







Related Posts

Codewars - KYU 8 - SQL Practice (10題)

Codewars - KYU 8 - SQL Practice (10題)

【Day05】使用 VS Code 修改內容製作個人化網頁

【Day05】使用 VS Code 修改內容製作個人化網頁

政大傳院院導師活動講座心得:余佩真的多重宇宙 影視歌三棲的創作人生

政大傳院院導師活動講座心得:余佩真的多重宇宙 影視歌三棲的創作人生


Comments